//
//  OneDayItemCollectionCell.swift
//  YOKO
//
//  Created by Zoe_G on 16/5/4.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit

let MonthViewEventCellHeight: CGFloat = 12.0

class OneDayItemCollectionCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
	@IBOutlet weak var dateLabel: UILabel!

	@IBOutlet weak var lunaLabel: UILabel!

	@IBOutlet weak var eventTableView: UITableView!

    @IBOutlet weak var maxNumLabel: UILabel!
    
    @IBOutlet weak var labelBackgroundView: UIView!
    
    @IBOutlet weak var plusBackgroundView: UIView!
	var date: NSDate?
    
    var maxEventNum: Int = 5

    var eventList = [EventInMonthView]() {
        didSet {
            self.eventTableView.reloadData()
        }
    }

	let cellId = "MonthViewEventcell"

	override func awakeFromNib() {

		self.backgroundColor = UIColor.whiteColor()

		lunaLabel.numberOfLines = 0
        
        plusBackgroundView.layer.cornerRadius = 4.0

		self.eventTableView.delegate = self
		self.eventTableView.dataSource = self

		self.eventTableView.registerNib(UINib.init(nibName: "MonthViewEventcell", bundle: nil), forCellReuseIdentifier: cellId)
        
        self.eventTableView.allowsSelection = false

	}

	func dateStringForCell() {
		if date != nil {

			lunaLabel.text = NSDate.getLunaString(date!)

			dateLabel.text = String(format: "%02d", NSDate.day(date!))

			if NSCalendar.currentCalendar().isDateInToday(date!) {
				self.layer.borderWidth = 1.0
				self.layer.borderColor = YOKOColor.TypeColors[1].CGColor
			} else {
				self.layer.borderWidth = 0
			}
		}

	}
    

	// MARK: - tableViewDalegate TODO
	func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.eventList.count == 0 {
            return 0
        } else {
            return maxEventNum
        }
    }

	func numberOfSectionsInTableView(tableView: UITableView) -> Int {
		return 1
	}

	func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
		return MonthViewEventCellHeight
	}

	func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
		let cell = tableView.dequeueReusableCellWithIdentifier(cellId) as! MonthViewEventcell
        
        let event = self.eventList[indexPath.row]

        if indexPath.row == maxEventNum-1 && event.eventLineType == CellLineType.LineMiddle{
            event.eventLineType = CellLineType.LineLast
        }
        
		cell.event = event
        
		return cell
	}


}
